home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Microsoft Plateform / Visual Basic 5.0 / Msvb50.ace / msvb50 / MSVB50 / VB / SAMPLES / VISDATA / ODBCLOGN.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-12-21  |  13.3 KB  |  432 lines

  1. VERSION 5.00
  2. Begin VB.Form frmODBCLogon 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "ODBC Logon"
  5.    ClientHeight    =   3180
  6.    ClientLeft      =   2850
  7.    ClientTop       =   1755
  8.    ClientWidth     =   4470
  9.    ControlBox      =   0   'False
  10.    BeginProperty Font 
  11.       Name            =   "Tahoma"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   400
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    HelpContextID   =   2016138
  20.    Icon            =   "ODBCLogn.frx":0000
  21.    LinkTopic       =   "Form1"
  22.    LockControls    =   -1  'True
  23.    MaxButton       =   0   'False
  24.    MinButton       =   0   'False
  25.    ScaleHeight     =   3180
  26.    ScaleWidth      =   4470
  27.    ShowInTaskbar   =   0   'False
  28.    StartUpPosition =   2  'CenterScreen
  29.    Begin VB.CommandButton cmdRegister 
  30.       Caption         =   "&Register"
  31.       Height          =   450
  32.       Left            =   120
  33.       MaskColor       =   &H00000000&
  34.       TabIndex        =   15
  35.       Top             =   2655
  36.       Width           =   1440
  37.    End
  38.    Begin VB.CommandButton cmdCancel 
  39.       Cancel          =   -1  'True
  40.       Caption         =   "Cancel"
  41.       Height          =   450
  42.       Left            =   3075
  43.       MaskColor       =   &H00000000&
  44.       TabIndex        =   13
  45.       Top             =   2655
  46.       Width           =   1260
  47.    End
  48.    Begin VB.CommandButton cmdOK 
  49.       Caption         =   "&OK"
  50.       Default         =   -1  'True
  51.       Height          =   450
  52.       Left            =   1740
  53.       MaskColor       =   &H00000000&
  54.       TabIndex        =   12
  55.       Top             =   2655
  56.       Width           =   1260
  57.    End
  58.    Begin VB.Frame fraConnection 
  59.       Caption         =   "Connection Values"
  60.       Height          =   2415
  61.       Left            =   120
  62.       TabIndex        =   14
  63.       Top             =   120
  64.       Width           =   4230
  65.       Begin VB.TextBox txtUID 
  66.          Height          =   300
  67.          Left            =   1125
  68.          TabIndex        =   3
  69.          Top             =   600
  70.          Width           =   3015
  71.       End
  72.       Begin VB.TextBox txtPWD 
  73.          Height          =   300
  74.          IMEMode         =   3  'DISABLE
  75.          Left            =   1125
  76.          PasswordChar    =   "*"
  77.          TabIndex        =   5
  78.          Top             =   930
  79.          Width           =   3015
  80.       End
  81.       Begin VB.TextBox txtDatabase 
  82.          Height          =   300
  83.          Left            =   1125
  84.          TabIndex        =   7
  85.          Top             =   1260
  86.          Width           =   3015
  87.       End
  88.       Begin VB.ComboBox cboDSNList 
  89.          Height          =   315
  90.          ItemData        =   "ODBCLogn.frx":000C
  91.          Left            =   1125
  92.          List            =   "ODBCLogn.frx":000E
  93.          Sorted          =   -1  'True
  94.          TabIndex        =   1
  95.          Text            =   "
  96.          Top             =   240
  97.          Width           =   3000
  98.       End
  99.       Begin VB.TextBox txtServer 
  100.          Enabled         =   0   'False
  101.          Height          =   330
  102.          Left            =   1125
  103.          TabIndex        =   11
  104.          Top             =   1935
  105.          Width           =   3015
  106.       End
  107.       Begin VB.ComboBox cboDrivers 
  108.          Enabled         =   0   'False
  109.          Height          =   315
  110.          ItemData        =   "ODBCLogn.frx":0010
  111.          Left            =   1125
  112.          List            =   "ODBCLogn.frx":0012
  113.          Sorted          =   -1  'True
  114.          Style           =   2  'Dropdown List
  115.          TabIndex        =   9
  116.          Top             =   1590
  117.          Width           =   3015
  118.       End
  119.       Begin VB.Label lblLabels 
  120.          AutoSize        =   -1  'True
  121.          Caption         =   "&DSN:"
  122.          Height          =   195
  123.          Index           =   0
  124.          Left            =   135
  125.          TabIndex        =   0
  126.          Top             =   285
  127.          Width           =   360
  128.       End
  129.       Begin VB.Label lblLabels 
  130.          AutoSize        =   -1  'True
  131.          Caption         =   "&UID:"
  132.          Height          =   195
  133.          Index           =   1
  134.          Left            =   135
  135.          TabIndex        =   2
  136.          Top             =   630
  137.          Width           =   330
  138.       End
  139.       Begin VB.Label lblLabels 
  140.          AutoSize        =   -1  'True
  141.          Caption         =   "&Password:"
  142.          Height          =   195
  143.          Index           =   2
  144.          Left            =   135
  145.          TabIndex        =   4
  146.          Top             =   975
  147.          Width           =   750
  148.       End
  149.       Begin VB.Label lblLabels 
  150.          AutoSize        =   -1  'True
  151.          Caption         =   "Data&base:"
  152.          Height          =   195
  153.          Index           =   3
  154.          Left            =   135
  155.          TabIndex        =   6
  156.          Top             =   1320
  157.          Width           =   750
  158.       End
  159.       Begin VB.Label lblLabels 
  160.          AutoSize        =   -1  'True
  161.          Caption         =   "Dri&ver:"
  162.          Enabled         =   0   'False
  163.          Height          =   195
  164.          Index           =   4
  165.          Left            =   135
  166.          TabIndex        =   8
  167.          Top             =   1665
  168.          Width           =   495
  169.       End
  170.       Begin VB.Label lblLabels 
  171.          AutoSize        =   -1  'True
  172.          Caption         =   "&Server:"
  173.          Enabled         =   0   'False
  174.          Height          =   195
  175.          Index           =   5
  176.          Left            =   135
  177.          TabIndex        =   10
  178.          Top             =   2010
  179.          Width           =   540
  180.       End
  181.    End
  182. Attribute VB_Name = "frmODBCLogon"
  183. Attribute VB_GlobalNameSpace = False
  184. Attribute VB_Creatable = False
  185. Attribute VB_PredeclaredId = True
  186. Attribute VB_Exposed = False
  187. Option Explicit
  188. '>>>>>>>>>>>>>>>>>>>>>>>>
  189. Const FORMCAPTION = "ODBC Logon"
  190. Const BUTTON1 = "&OK"
  191. Const BUTTON2 = "&Cancel"
  192. Const BUTTON3 = "&Register"
  193. Const FRAME1 = "Connect Values:"
  194. Const Label1 = "&DSN:"
  195. Const Label2 = "&UID:"
  196. Const LABEL3 = "&Password:"
  197. Const LABEL4 = "Data&base:"
  198. Const LABEL5 = "Dri&ver:"
  199. Const LABEL6 = "&Server:"
  200. Const MSG1 = "Enter ODBC Connection Parameters"
  201. Const MSG2 = "Opening ODBC Database"
  202. Const MSG3 = "Enter Driver Name from ODBCINST.INI File:"
  203. Const MSG4 = "Driver Name"
  204. Const MSG5 = "This Datasource has not been Registered, this will now be attempted for you!"
  205. Const MSG7 = "Invalid Parameter(s), Please try again!"
  206. Const MSG8 = "Query Timeout Could not be set, default will be used!"
  207. Const MSG9 = "Datasource Registration Succeeded, proceed with Open."
  208. Const MSG10 = "Please enter a DSN!"
  209. Const MSG11 = "Please select a Driver!"
  210. Const MSG12 = "You must Close First!"
  211. '>>>>>>>>>>>>>>>>>>>>>>>>
  212. Dim mbBeenLoaded As Integer
  213. Public DBOpened As Boolean
  214. Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
  215. Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
  216. Const SQL_SUCCESS As Long = 0
  217. Const SQL_FETCH_NEXT As Long = 1
  218. Private Sub cboDSNList_Change()
  219.   If Len(cboDSNList.Text) = 0 Or cboDSNList.Text = "(None)" Then
  220.     txtServer.Enabled = True
  221.     cboDrivers.Enabled = True
  222.     lblLabels(4).Enabled = True
  223.     lblLabels(5).Enabled = True
  224.   Else
  225.     txtServer.Enabled = False
  226.     cboDrivers.Enabled = False
  227.     lblLabels(4).Enabled = False
  228.     lblLabels(5).Enabled = False
  229.   End If
  230. End Sub
  231. Private Sub cmdCancel_Click()
  232.   gbDBOpenFlag = False
  233.   gsDBName = vbNullString
  234.   DBOpened = False
  235.   Me.Hide
  236. End Sub
  237. Private Sub cmdOK_Click()
  238.   On Error GoTo cmdOK_ClickErr
  239.   Dim sConnect As String
  240.   Dim dbTemp As Database
  241.   MsgBar MSG2, True
  242.   If frmMDI.mnuPOpenOnStartup.Checked Then
  243.     Me.Refresh
  244.   End If
  245.   Screen.MousePointer = vbHourglass
  246.   If Len(cboDSNList.Text) > 0 Then
  247.     sConnect = "ODBC;DSN=" & cboDSNList.Text & ";"
  248.   Else
  249.     sConnect = "ODBC;Driver={" & cboDrivers.Text & "};"
  250.     sConnect = sConnect & "Server=" & txtServer.Text & ";"
  251.   End If
  252.   sConnect = sConnect & "UID=" & txtUID.Text & ";"
  253.   sConnect = sConnect & "PWD=" & txtPWD.Text & ";"
  254.   If Len(txtDatabase.Text) > 0 Then
  255.     sConnect = sConnect & "Database=" & txtDatabase.Text & ";"
  256.   End If
  257.   Set dbTemp = gwsMainWS.OpenDatabase("", 0, 0, sConnect)
  258.   If gbDBOpenFlag Then
  259.     CloseCurrentDB
  260.     If gbDBOpenFlag Then
  261.       Beep
  262.       MsgBox MSG12, 48
  263.       Me.Hide
  264.       Exit Sub
  265.     End If
  266.   End If
  267.   'success
  268.   DBOpened = True
  269.   'save the values
  270.   gsODBCDatasource = cboDSNList.Text
  271.   gsDBName = gsODBCDatasource
  272.   gsODBCDatabase = txtDatabase.Text
  273.   gsODBCUserName = txtUID.Text
  274.   gsODBCPassword = txtPWD.Text
  275.   gsODBCDriver = cboDrivers.Text
  276.   gsODBCServer = txtServer.Text
  277.   gsDataType = gsSQLDB
  278.   Set gdbCurrentDB = dbTemp
  279.   GetODBCConnectParts gdbCurrentDB.Connect
  280.   cboDSNList.Text = gsODBCDatasource
  281.   txtDatabase.Text = gsODBCDatabase
  282.   txtUID.Text = gsODBCUserName
  283.   txtPWD.Text = gsODBCPassword
  284.   frmMDI.Caption = "VisData:" & gsDBName & "." & gsODBCDatabase
  285.   gdbCurrentDB.QueryTimeout = glQueryTimeout
  286.   gbDBOpenFlag = True
  287.   AddMRU
  288.   Screen.MousePointer = vbDefault
  289.   Me.Hide
  290.   Exit Sub
  291. cmdOK_ClickErr:
  292.   Screen.MousePointer = vbDefault
  293.   gbDBOpenFlag = False
  294.   If Len(cboDSNList.Text) > 0 Then
  295.     If InStr(1, Error, "ODBC--connection to '" & cboDSNList.Text & "' failed") > 0 Then
  296.       Beep
  297.       MsgBox MSG5, 48
  298.       txtDatabase.Text = vbNullString
  299.       txtUID.Text = vbNullString
  300.       txtPWD.Text = vbNullString
  301.       If RegisterDB((cboDSNList.Text)) Then
  302.         MsgBox MSG9, 48
  303.       End If
  304.     ElseIf InStr(1, Error, "Login failed") > 0 Then
  305.       Beep
  306.       MsgBox MSG7, 48
  307.     ElseIf InStr(1, Error, "QueryTimeout property") > 0 Then
  308.       If glQueryTimeout <> 5 Then
  309.         Beep
  310.         MsgBox MSG8, 48
  311.       End If
  312.       Resume Next
  313.     Else
  314.       ShowError
  315.     End If
  316.   End If
  317.   MsgBar MSG1, False
  318.   If Err = 3059 Then
  319.     Unload Me
  320.   End If
  321. End Sub
  322. Private Sub cmdRegister_Click()
  323.   On Error GoTo cmdRegister_ClickErr
  324.   If Len(cboDSNList.Text) = 0 Then
  325.     MsgBox MSG10, vbInformation, Me.Caption
  326.     Exit Sub
  327.   End If
  328.   If Len(cboDrivers.Text) = 0 Then
  329.     MsgBox MSG11, vbInformation, Me.Caption
  330.     Exit Sub
  331.   End If
  332.   'try to register it
  333.   DBEngine.RegisterDatabase cboDSNList.Text, cboDrivers.Text, False, vbNullString
  334.   MsgBox MSG9, vbInformation
  335.   Exit Sub
  336. cmdRegister_ClickErr:
  337.   ShowError
  338. End Sub
  339. Private Sub Form_Load()
  340.   Dim i As Integer
  341.   Me.Caption = FORMCAPTION
  342.   cmdOK.Caption = BUTTON1
  343.   cmdCancel.Caption = BUTTON2
  344.   cmdRegister.Caption = BUTTON3
  345.   fraConnection.Caption = FRAME1
  346.   lblLabels(0).Caption = Label1
  347.   lblLabels(1).Caption = Label2
  348.   lblLabels(2).Caption = LABEL3
  349.   lblLabels(3).Caption = LABEL4
  350.   lblLabels(4).Caption = LABEL5
  351.   lblLabels(5).Caption = LABEL6
  352.   GetDSNsAndDrivers
  353.   MsgBar MSG1, False
  354.   cboDSNList.Text = gsODBCDatasource
  355.   txtDatabase.Text = gsODBCDatabase
  356.   txtUID.Text = gsODBCUserName
  357.   txtPWD.Text = gsODBCPassword
  358.   If Len(gsODBCDriver) > 0 Then
  359.     For i = 0 To cboDrivers.ListCount - 1
  360.       If cboDrivers.List(i) = gsODBCDriver Then
  361.         cboDrivers.ListIndex = i
  362.         Exit For
  363.       End If
  364.     Next
  365.   End If
  366.   txtServer.Text = gsODBCServer
  367.   mbBeenLoaded = True
  368. End Sub
  369. Private Sub cboDSNList_Click()
  370.   cboDSNList_Change
  371. End Sub
  372. Sub GetDSNsAndDrivers()
  373.   On Error Resume Next
  374.   Dim i As Integer
  375.   Dim sDSNItem As String * 1024
  376.   Dim sDRVItem As String * 1024
  377.   Dim sDSN As String
  378.   Dim sDRV As String
  379.   Dim iDSNLen As Integer
  380.   Dim iDRVLen As Integer
  381.   Dim lHenv As Long     'handle to the environment
  382.   cboDSNList.AddItem "(None)"
  383.   'get the DSNs
  384.   If SQLAllocEnv(lHenv) <> -1 Then
  385.     Do Until i <> SQL_SUCCESS
  386.       sDSNItem = Space(1024)
  387.       sDRVItem = Space(1024)
  388.       i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
  389.       sDSN = VBA.Left(sDSNItem, iDSNLen)
  390.       sDRV = VBA.Left(sDRVItem, iDRVLen)
  391.         
  392.       If sDSN <> Space(iDSNLen) Then
  393.         cboDSNList.AddItem sDSN
  394.         cboDrivers.AddItem sDRV
  395.       End If
  396.     Loop
  397.   End If
  398.   'remove the dupes
  399.   If cboDSNList.ListCount > 0 Then
  400.     With cboDrivers
  401.       If .ListCount > 1 Then
  402.         i = 0
  403.         While i < .ListCount
  404.           If .List(i) = .List(i + 1) Then
  405.             .RemoveItem (i)
  406.           Else
  407.             i = i + 1
  408.           End If
  409.         Wend
  410.       End If
  411.     End With
  412.   End If
  413.   cboDSNList.ListIndex = 0
  414. End Sub
  415. Private Sub Form_Unload(Cancel As Integer)
  416.   MsgBar vbNullString, False
  417. End Sub
  418. Private Function RegisterDB(rsDatasource As String) As Integer
  419.    On Error GoTo RDBErr
  420.    Dim sDriver As String
  421.    sDriver = InputBox(MSG3, MSG4, gsDEFAULT_DRIVER)
  422.    If sDriver <> gsDEFAULT_DRIVER Then
  423.      DBEngine.RegisterDatabase rsDatasource, sDriver, False, vbNullString
  424.    Else
  425.      DBEngine.RegisterDatabase rsDatasource, sDriver, True, vbNullString
  426.    End If
  427.    RegisterDB = True
  428.    Exit Function
  429. RDBErr:
  430.    RegisterDB = False
  431. End Function
  432.